/****************************************************************************
 *
 * Copyright (C) 2003-2009 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
/**
 * 
 */
package nfse.dataset.r3000;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.sql.*;
import java.util.Scanner;
import java.util.ArrayList;
import nfse.*;
import nfse.gui.record.*;

/**
 * @author bduphoff
 * 
 */
public class R3000RecordTest extends NetFSERecordTest {

	private final String fileName = "data/r3000.txt";
	private final int numLogs = 100;
	private final int sensorID = 5;
	private R3000RecordFactory rec = new R3000RecordFactory();
	ArrayList<NetFSERecord> records = null;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		Scanner in = new Scanner(new File(fileName));
		while (in.hasNext()) {
			logs.add(in.nextLine());
		}
		in.close();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {

	}

	/**
	 * Test method for {@link nfse.dataset.lanl.R3000Record#getFlexRecord()}.
	 */
	@Test
	public void testGetFlexRecord() {
		ArrayList<NetFSERecord> records = getRecords();
		for (NetFSERecord record : records) {
			R3000Record temp = (R3000Record)record;
			FlexRecord fr = rec.getFlexRecord(temp);
			assertEquals(fr.sensorID, getSensorID());
			assertEquals(fr.values[0], record.getStartTS().toString());
			assertEquals(fr.values[1], record.getSrcAddr());
			assertEquals(fr.values[2], temp.type);
			assertEquals(fr.values[3], temp.category);
			assertEquals(fr.values[4], temp.urlBase);
			assertEquals(fr.values[5], temp.url);
		}
	}

	/**
	 * Test method for {@link nfse.dataset.lanl.R3000Record#getAlertMessage()}.
	 */
	@Test
	public void testGetAlertMessage() {
		NetFSERecord rec = getRecord();
		assertTrue(rec.getAlertMessage() == null || rec.getAlertMessage().length() == 0);
	}
	
	/**
	 * Test method for
	 * {@link nfse.dataset.lanl.R3000Record#getFlexRecordFields()}.
	 */
	@Test
	public void testGetFlexRecordFields() {
		String[] names = { "startts", "srcip", "type", "category", "urlbase",
				"url" };
		FlexRecordField[] fields = rec.getFlexRecordFields();
		assertEquals("Excpected " + names.length + " fields.", names.length,
				fields.length);
		for (int i = 0; i < fields.length; i++) {
			assertEquals(names[i], fields[i].columnName);
			// System.out.println(fields[i].columnName);
		}
	}

	/**
	 * Test method for {@link nfse.dataset.lanl.R3000Record#getIndexedFields()}.
	 */
	@Test
	public void testGetIndexedFields() {
		int[] fields = getRecord().getIndexedFields();
		assertEquals("startts", NetFSE.getFieldName(fields[0]));
		assertEquals("srcip", NetFSE.getFieldName(fields[1]));
	}

	/**
	 * Test method for {@link nfse.dataset.lanl.R3000Record#isAlert()}.
	 */
	@Test
	public void testIsAlert() {
		assertFalse(rec.generate().isAlert());
	}

	/**
	 * Test method for
	 * {@link nfse.dataset.lanl.R3000Record#generate(java.lang.String, int)}.
	 */
	@Test
	public void testGenerateStringInt() {
		getRecords();
	}

	public ArrayList<NetFSERecord> getRecords() {
		records = new ArrayList<NetFSERecord>();
		int count = 0;
		for (String log : logs) {
			R3000Record temp = (R3000Record)rec.generate(log, sensorID);
			if (temp != null) {
				count++;
				records.add(temp);
			} else
				fail("Could not parse '" + log + "'");
			// System.out.println(temp.toCSV());
		}
		if (count != numLogs)
			fail("Expected " + numLogs + " but only parsed " + count + " from "
					+ fileName);
		return records;
	}

	/**
	 * Test attribute defaults
	 */
	@Test
	public void testDefaults() {
		R3000Record rec = new R3000Record();
		assertTrue(rec.url.length() == 0);
		assertTrue(rec.urlBase.length() == 0);
		assertTrue(rec.category.length() == 0);
		assertTrue(rec.type.length() == 0);
	}

	/**
	 * Test method for {@link nfse.NetFSERecord#getEndTS()}.
	 */
	@Test
	public void testGetEndTS() {
		ArrayList<NetFSERecord> records = getRecords();
		for (NetFSERecord record : records) {
			assertEquals(record.getStartTS(), record.getEndTS());
		}
	}

	/**
	 * Test method for {@link nfse.NetFSERecord#setStartTS(java.sql.Timestamp)}.
	 */
	@Test
	public void testStartTS() {
		NetFSERecord rec = getRecord();
		long ts = 100000000;
		Timestamp ts2 = new Timestamp(ts);
		rec.setStartTS(ts2);
		assertEquals(ts2, rec.getStartTS());
		assertEquals(ts2, rec.getEndTS());
		assertEquals(ts2, rec.getTimestamp());
	}

	@Override
	protected String getFileName() {
		return fileName;
	}

	@Override
	protected NetFSERecord getRecord() {
		if (records != null)
			return records.get(0);
		getRecords();
		return records.get(0);
	}

	@Override
	protected int getSensorID() {
		return sensorID;
	}

	/**
	 * Test method for {@link nfse.NetFSERecord#isSyslog()}.
	 */
	@Test
	public void testIsSyslog() {
		assertFalse(rec.isSyslog());
	}

}
